package com.open.harmony.controller;

import com.open.harmony.common.Result;
import com.open.harmony.business.entity.Product;
import com.open.harmony.business.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.open.harmony.common.image.ImageDao;


@Api(tags = "商品模块")
@RequestMapping("/product")
@RestController
public class ProductController {
    private final ProductService productService;

    private final ImageDao imageDao;

    @Autowired
    public ProductController(ProductService productService, ImageDao imageDao) {
        this.productService = productService;
        this.imageDao = imageDao;
    }

    /**
     * 查询所有商品
     *
     * @return 所有商品
     */
    @ApiOperation("查询所有商品")
    @GetMapping("/findAll")
    public Result selectAll() {
        return productService.findAll();
    }

    /**
     * 根据商户id查询所有商品
     *
     * @return 该商家商品
     */
    @ApiOperation("根据商户id查询所有商品")
    @GetMapping("/findAllByStoreId")
    public Result selectAllByStoreId(Integer storeId) {
        return productService.findAllByStoreId(storeId);
    }

    /**
     * 更新商品
     *
     * @param product 根据id更新商品
     * @return 更新成功
     */
    @ApiOperation("更改指定商品")
    @PutMapping("/updateProduct")
    public Result updateProduct(@RequestBody Product product) {
        return productService.updateProduct(product);
    }

    /**
     * 添加商品
     *
     * @param product 要添加的商品信息
     * @return 添加状态
     */
    @ApiOperation("添加商品")
    @PutMapping("/addProduct")
    public Result addGoods(@RequestBody Product product) {
        return productService.insertProduct(product);
    }

    /**
     * 上传图片到zimg
     * @param file 图片文件
     * @return 图片的md5
     */
    @ApiOperation(value = "/photo", httpMethod = "POST")
    @PostMapping("/addPhoto")
    public Result uploadPhoto(@RequestParam("file") MultipartFile file){
        return imageDao.upload(file);
    }


    /**
     * 下架该商品
     * @param productId 商品Id
     * @return 状态
     */
    @Transactional
    @ApiOperation("删除（下架）商品")
    @PostMapping("/deleteProduct")
    public Result deleteProduct(Integer productId) {
        return productService.deleteProduct(productId);
    }
}
